<?php

//检测是否有可以进行测评的项目
function get_usermodel(){
	//初始化值
	$userid='';//用户id
	$hpgroup='';//互评组
	$cpgroup='';//测评组
	$grouparray='';//所有评论的组

	$checkid=0;//返回值

	
	if(isset($_SESSION['userid'])){
		$userid=$_SESSION['userid'];
		$c="userid='".$userid."' and appraisaled = 1 ";
		$hpgroup=getdataallfdiyc('groupid','app_user',$c);
		$cpgroup=getdataallfdiyc('groupid','app_user_sup',$c);
		if(is_array($hpgroup)){
			$grouparray[1]=$hpgroup;
		}
		if(is_array($cpgroup)){
			$grouparray[2]=$cpgroup;
		}
		
		if(is_array($grouparray)){
		foreach ($grouparray as $key => $value) {
			if (is_array($value)){
				foreach ($value as $vkey => $vvalue) {

					$groupid='';//组序号
					$groupname='';//组名称
					$sort='';//项目
					$sortid='';//项目序号
					$groupuser='';//组员
					$groupuserid='';//组员序号
					$thisgroupuser='';//评论的用户
					$checkif=0;//标志组中是否含有自己

					//获取组
					$groupid=$vvalue;
					$groupname=getdataf('groupname','app_group','groupid',$groupid);
					
					//获取组员
					$groupuserid=getdataallfc('userid','app_user','groupid',$groupid);
					foreach ($groupuserid as $tkey => $tvalue) {
						if($tvalue!=$userid){
							$checkif=1;
							$groupuser[$tvalue]=getdataf('username','usertable','userid',$tvalue);
						}
						else{
							$thisgroupuser[$userid]=getdataf('username','usertable','userid',$tvalue);
						}
					}
					
					//获取项目
					$sortid=getdataallfc('sortid','app_sort_group','groupid',$groupid);
					if(is_array($sortid)){	
					foreach ($sortid as $tkey => $tvalue) {
						$sorttype=get_fatersort($tvalue);
						$sort[$sorttype][$tvalue]=get_sort_xxnameu($tvalue);//getdataf('sortname','app_sort','sortid',$tvalue);
					}
				}
					//显示模型
					$check=0;
					$sortrule='';//项目规则
					$thissort='';//当前项目
					if($key==1){
						$sortrule['type']=$key;
						if(isset($sort[1])){
							$thisgroupname="自评-".$groupname;
							$thissort=$sort[1];
							$check=1;
						}
						if(isset($sort[2])){
							$thisgroupname="互评-".$groupname;
							$thissort=$sort[2];
							$check=1;
						}
						
					}else if($key==2){
						$sortrule['type']=$key;
						if(isset($sort[3])){
							$thisgroupname="测评-".$groupname;
							if($checkif){
								$groupuser[$userid]=$thisgroupuser[$userid];
							}
							
							$thissort=$sort[3];
							$check=1;		
						}
					}
					//显示项目表
					if($check){
						$checkid=1;
						project_model($thissort,$sortrule,$groupuser,$groupid,$thisgroupname);
					}
					else{
					}
					
					

				}
			}
		}
	}
	else{
		$checkid=2;
	}
	}
	else{
		echo"请先登录";
	}

	return $checkid;
}
/*
//获取项目的所有父节点,按顺序
function get_sortfather($sortid){
	$father[0]=getdataf("sortupid","app_sort","sortid",$sortid);
	$i=1;
	while ($father[$i-1]!=0) {	
		$father[$i]=getdataf("sortupid","app_sort","sortid",$father[$i-1]);
		$i++;
	}
	return $father;
}
*/
//获取项目详细名字
function get_sort_xxnameu($sortid){
	$father=get_sortfather($sortid);
	$sortname=getdataf("sortname","app_sort","sortid",$sortid);
	foreach ($father as $key => $value) {
		if($value>3)
		{
			$sortname=getdataf("sortname","app_sort","sortid",$value)."-".$sortname;
		}
	}
	return $sortname;
}
	

//获取项目的类型
function get_fatersort($sortid){
	$fsortid=getdataf('sortupid','app_sort','sortid',$sortid);
	if($fsortid==2){
		return 2;
	}
	else if($fsortid==1){
		return 1;
	}
	else if($fsortid==3){
		return 3;
	}
	else{
		return get_fatersort($fsortid);
	}
}

//评分
//直接使用了数据库操作语句
function insert_grade($userid,$sortid,$groupid,$sortpoint)
{
	//确认是否已存在
	$num_row=0;
	$query="select * from app_grade where userid='".$userid."' and sortid=".$sortid." and groupid='".$groupid."';";
	$result=mysql_query($query)or die(mysql_error());
	$num_row=mysql_num_rows($result);

	if($num_row){
		//update
		//echo "更新操作";
		$query="update app_grade set point=point+".$sortpoint." where userid='".$userid."' and sortid=".$sortid." and groupid='".$groupid."';";
		$result=mysql_query($query) or die(mysql_error());
		if($result){
			return 1;
		}
		else{
			return 0;
		}
	}
	else{
		//insert
		//echo "插入操作";
		$condition="'".$userid."',".$sortid.",'".$groupid."',".$sortpoint;
		$rel=insertdata('app_grade','userid,sortid,groupid,point',$condition);
		if($rel){
			//echo "插入成功";
			return 1;
		}
		else{
			//echo "插入失败";
			return 0;
		}
	}	
	
}
	
//直接使用了数据库操作语句
//修改测评标志
function user_apped($userid,$groupid,$table){
	$query="update ".$table." set appraisaled = 0 where userid='".$userid."' and groupid='".$groupid."';";
	$rel=mysql_query($query)or die(mysql_error());
}
	


	
	


?>